package com.example.mybatisplus.service.impl;

import ch.qos.logback.core.util.COWArrayList;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.model.domain.BedManagment;
import com.example.mybatisplus.mapper.BedManagmentMapper;
import com.example.mybatisplus.model.dto.PageDTO;
import com.example.mybatisplus.model.vo.StatVO;
import com.example.mybatisplus.service.BedManagmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ablbb
 * @since 2024-07-18
 */
@Service
public class BedManagmentServiceImpl extends ServiceImpl<BedManagmentMapper, BedManagment> implements BedManagmentService {

    @Override
    public Page<BedManagment> pageList(BedManagment bedManagment, PageDTO dto) {
        QueryWrapper<BedManagment> wrapper = new QueryWrapper<>();
        if(bedManagment.getDigit() != null){
            wrapper.like("digit",bedManagment.getDigit());
        }
        if(bedManagment.getBedNumber() != null){
            wrapper.like("bed_number",bedManagment.getBedNumber());
        }
        if(bedManagment.getPrice() != null){
            wrapper.like("price",bedManagment.getPrice());
        }
        if(bedManagment.getFloor() != null){
            wrapper.like("floor",bedManagment.getFloor());
        }
        if(StringUtils.isNotBlank(bedManagment.getPrefix())){
            wrapper.like("prefix",bedManagment.getPrefix());
        }
        Page<BedManagment> page = new Page<>();
        page.setCurrent(dto.getPageNo()).setSize(dto.getPageSize());
        baseMapper.selectPage(page,wrapper);
        return page;
    }

    public void generateRooms(String buildingId, int floor, int roomNumber) {
        List<BedManagment> rooms = new ArrayList<>();
        for (int i = 1; i <= roomNumber; i++) {
            BedManagment room = new BedManagment();
            room.setRoomId(buildingId + floor + String.format("%02d", i));
            room.setBuildingId(buildingId);
            room.setRoomNumber(Integer.valueOf(String.valueOf(roomNumber)));
            room.setFloor(floor);
            room.setBedNumber(6); // 假设每个房间有6张床
            room.setPrice(1000); // 假设默认价格为1000
            room.setDigit(floor * 100 + i);
            room.setPrefix(buildingId);
            room.setUrl(null);
            rooms.add(room);
        }
        baseMapper.insertBatch(rooms);
    }



    @Override
    public List<StatVO> statbed() {
        return baseMapper.statbed();
    }
}
